Game xếp hình 2048

13.194 lượt xem;
1 using UnityEngine;
2 using
System.Collections;
3
4 public
class SwipeListenerScript : MonoBehaviour {
5     
6     
private Vector2 startPosition; // first finger position
7     
private Vector2 lastPosition; // last finger position
8
9     
10     
void Update () {
11         
//transform.guiText.text = "my text";
12         
int fingerCount = 0;
13         
foreach (Touch touch in Input.touches) {
14             
if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
15                 fingerCount++;
16
17             
if (touch.phase == TouchPhase.Began)
18             {
19                 startPosition = touch.position;
20                 lastPosition = touch.position;
21             }
22
23             
if (touch.phase == TouchPhase.Moved )
24             {
25                 lastPosition = touch.position;
26             }
27             
if(touch.phase == TouchPhase.Ended)
28             {
29                 
30                 
if((startPosition.x - lastPosition.x) > 80) // left swipe
31                 {
32                     
this.Swipe ("x", -1);
33                 }
34                 
else if((startPosition.x - lastPosition.x) < -80) // right swipe
35                 {
36                     
this.Swipe ("x", 1);
37                 }
38                 
else if((startPosition.y - lastPosition.y) < -80 ) // up swipe
39                 {
40                     
this.Swipe ("y", 1);
41                 }
42                 
else if((startPosition.y - lastPosition.y) > 80 ) // down swipe
43                 {
44                     
this.Swipe ("y", -1);
45                 }
46             
47             }
48         }
49     }
50
51     
void Swipe(string axis, int direction) {
52         GameControllerScript gameScript =
this.GetComponent ("GameControllerScript") as GameControllerScript;
53         
if (gameScript.gameView == "game") gameScript.doMove (axis, direction);
54     }
55 }


private Vector2 startPosition; first finger position

private Vector2 lastPosition; last finger position

transform.guiText.text = "my text";

if((startPosition.x - lastPosition.x) > 80) left swipe

else if((startPosition.x - lastPosition.x) < -80) right swipe

else if((startPosition.y - lastPosition.y) < -80 ) up swipe

else if((startPosition.y - lastPosition.y) > 80 ) down swipe



Gõ tìm kiếm nhanh...